const db = require("../db/util");

//获取文章分类列表
let getArticleCates = (req, res) => {
  const sql =
    "select * from ev_article_cate where is_delete=0 order by id asc ";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err.message);

    if (results.length == 0) return res.cc("未搜索到任何数据");

    res.send({
      status: 0,
      message: "获取成功",
      data: results,
    });
  });
};

//添加图书
let addArticleCates = (req, res) => {
  //判断文章分类是否存在
  let sql = "select * from ev_article_cate where name=? or alias=?";
  db.query(sql, [req.body.name, req.body.alias], (err, results) => {
    if (err) return res.cc(err.message);
    if (results.length != 0) return res.cc("文章分类名称或别名被占用");

    //进行添加操作
    let sql = "INSERT INTO ev_article_cate set ?";
    db.query(sql, req.body, (err, results) => {
      if (err) return res.cc(err.message);
      if (results.affectedRows == 0) return res.cc("添加失败");

      res.cc("添加成功", 0);
    });
  });
};

//根据id删除文章分类
let deleteCateById = (req, res) => {
  //判断文章分类是否存在
  let sql = "select * from ev_article_cate where id=?";
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err.message);
    if (results.length == 0) return res.cc("文章分类不存在");

    //进行删除操作
    let sql = "update ev_article_cate set is_delete=1 where id=?";
    db.query(sql, req.params.id, (err, results) => {
      if (err) return res.cc(err.message);
      if (results.affectedRows == 0) return res.cc("删除失败");
      res.cc("删除成功", 0);
    });
  });
};


//根据id获取文章分类
let getArtCateById = (req, res) => {
  let sql = "SELECT * from ev_article_cate WHERE id = ?";
  db.query(sql,req.params.id, (err,results) => {
    if(err) return res.cc(err.message)
    if(results.length == 0) return res.cc("未查询到有效信息")

    res.send({
      status:0,
      message:"查询成功",
      data:results[0],
    })
  })
}

//根据id更新文章分类
let updateCateById = (req, res) => {
  const sql = "select * from ev_article_cate where Id<>? and (name=? or alias=?)"
  db.query(sql, [req.body.Id, req.body.name, req.body.alias], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
  
    // 分类名称 和 分类别名 都被占用
    if (results.length === 2) return res.cc('分类名称与别名被占用，请更换后重试！')
    if (results.length === 1 && results[0].name === req.body.name && results[0].alias === req.body.alias) return res.cc('分类名称与别名被占用，请更换后重试！')
    // 分类名称 或 分类别名 被占用
    if (results.length === 1 && results[0].name === req.body.name) return res.cc('分类名称被占用，请更换后重试！')
    if (results.length === 1 && results[0].alias === req.body.alias) return res.cc('分类别名被占用，请更换后重试！')
  
    const sql = "UPDATE ev_article_cate SET ? where id =? and is_delete = 0"
    db.query(sql,[req.body,req.body.Id], (err, results)=>{
      console.log(req.body);
      if(err) return res.cc(err.message)
      console.log(sql);
      if(results.affectedRows == 0) return res.cc("更新失败")
      res.cc("更新成功",0)
    })
  })
}
module.exports = {
  getArticleCates,
  addArticleCates,
  deleteCateById,
  getArtCateById,
  updateCateById,
};
